package com.yupi.springbootinit.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.springbootinit.common.QueryListWhenId;
import com.yupi.springbootinit.mapper.ConurseTopicMapper;
import com.yupi.springbootinit.model.entity.ConurseTopic;
import com.yupi.springbootinit.model.entity.ConurseTopicAnswer;
import com.yupi.springbootinit.model.entity.ConurseTopicContent;
import com.yupi.springbootinit.model.vo.ConurseTopicContentReponseBody;
import com.yupi.springbootinit.model.vo.ConurseTopicReponseBody;
import com.yupi.springbootinit.model.vo.CourseTopicAnswerReponseBody;
import com.yupi.springbootinit.service.ConurseTopicAnswerService;
import com.yupi.springbootinit.service.ConurseTopicContentService;
import com.yupi.springbootinit.service.ConurseTopicService;
import com.yupi.springbootinit.utils.DataConversion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

/**
* @author 15312
* @description 针对表【conurse_topic(题目表)】的数据库操作Service实现
* @createDate 2024-05-15 16:13:34
*/
@Service
public class ConurseTopicServiceImpl extends ServiceImpl<ConurseTopicMapper, ConurseTopic>
    implements ConurseTopicService{

    @Autowired
    private ConurseTopicMapper conurseTopicMapper;

    @Autowired
    private ConurseTopicContentService conurseTopicContentService;

    @Autowired
    private ConurseTopicAnswerService conurseTopicAnswerService;

    @Override
    public List<ConurseTopicReponseBody> queryConurseTopicReponseBodyList(QueryListWhenId queryListWhenId) {
        return DataConversion.conversionOnList(conurseTopicMapper.queryConurseTopicListByClassify(queryListWhenId.getId()), ConurseTopicReponseBody.class);
    }

    // 查题目和答案
    @Override
    public List<ConurseTopicContentReponseBody> queryTopicContentAndAnswerList(Long id) {
        try {
            // 获取题目
            CompletableFuture<List<ConurseTopicContentReponseBody>> content = CompletableFuture.supplyAsync(() -> {
                LambdaQueryWrapper<ConurseTopicContent> contentWrapper = new LambdaQueryWrapper<>();
                contentWrapper.eq(ConurseTopicContent::getTopicId, id);
                return DataConversion.conversionOnList(conurseTopicContentService.list(contentWrapper), ConurseTopicContentReponseBody.class);
            });
            // 获取答案
            CompletableFuture<List<CourseTopicAnswerReponseBody>> answer = CompletableFuture.supplyAsync(() -> {
                LambdaQueryWrapper<ConurseTopicAnswer> answerWrapper = new LambdaQueryWrapper<>();
                answerWrapper.eq(ConurseTopicAnswer::getTopicId, id);
                return DataConversion.conversionOnList(conurseTopicAnswerService.list(answerWrapper), CourseTopicAnswerReponseBody.class);
            });
            // 等待
            CompletableFuture.allOf(content, answer);
            if (CollectionUtil.isNotEmpty(content.get())) {
                if (CollectionUtil.isNotEmpty(answer.get())) {
                    try {
                        List<CourseTopicAnswerReponseBody> answers = answer.get();
                        content.get().forEach(item -> {
                            item.setAnswerList(answers.stream().filter(value -> Objects.equals(value.getTopicContentId(), item.getId())
                            ).collect(Collectors.toList()));
                        });
                    }catch (Exception e) {
                        return null;
                    }
                }
            }
            return content.get();
        }catch (ExecutionException | InterruptedException e) {
            return null;
        }
    }
}




